﻿namespace sfcms.api
{
    using System;
    using System.Data;
    using System.Web;

    public class page : child
    {
        private string _friendlyurl;
        private placeholder[] _placeholders;
        public string displayname;
        public string name;
        public string templatepath;

        internal page()
        {
        }

        public static page[] byparentid(long parentid)
        {
            DataTable pagesData = new database().ExecuteDataTable("SELECT * FROM SFCMS_PagePath PP (NOLOCK) JOIN SFCMS_Page P (NOLOCK) ON PP.PageID = P.[ID] WHERE PP.[ParentID] = @0 ORDER BY P.[Name]", new object[] { "@0", parentid });
            page[] pages = new page[pagesData.Rows.Count];
            for (int i = 0; i < pagesData.Rows.Count; i++)
            {
                pages[i] = new page();
                pages[i].id = Convert.ToInt64(pagesData.Rows[i]["PageID"]);
                pages[i].name = pagesData.Rows[i]["Name"].ToString();
                pages[i].displayname = pagesData.Rows[i]["DisplayName"].ToString();
                pages[i].templatepath = pagesData.Rows[i]["TemplatePath"].ToString();
            }
            return pages;
        }

        public static page byparentidanddisplayname(long parentid, string displayname)
        {
            page[] pages = byparentid(parentid);
            foreach (page subpage in pages)
            {
                if (urlrewriter.toasciilower(subpage.displayname) == urlrewriter.toasciilower(displayname))
                {
                    return subpage;
                }
            }
            return null;
        }

        public void delete()
        {
            new database().ExecuteNonQuery("DELETE SFCMS_PlaceHolder WHERE PageID = @0\r\nDELETE SFCMS_Page WHERE [ID] = @0", new object[] { "@0", base.id });
        }

        public static page fromid(long id)
        {
            DataTable pageData = new database().ExecuteDataTable("SELECT * FROM SFCMS_Page (NOLOCK) WHERE [ID] = @0", new object[] { "@0", id });
            if (pageData.Rows.Count == 0)
            {
                return null;
            }
            return new page { id = id, name = pageData.Rows[0]["Name"].ToString(), displayname = pageData.Rows[0]["DisplayName"].ToString(), templatepath = pageData.Rows[0]["TemplatePath"].ToString() };
        }

        public void save()
        {
            if (base.id == 0L)
            {
                base.id = Convert.ToInt64(new database().ExecuteScalar("INSERT INTO SFCMS_Page ([Name], [DisplayName], [TemplatePath]) VALUES(@0, @1, @2)\r\nSELECT SCOPE_IDENTITY()", new object[] { "@0", this.name, "@1", this.displayname, "@2", _templatepath }));
            }
            else
            {
                new database().ExecuteNonQuery("UPDATE SFCMS_Page SET [Name] = @0, DisplayName = @1 WHERE [ID] = @2", new object[] { "@0", this.name, "@1", this.displayname, "@2", base.id });
            }
        }

        private static string _templatepath
        {
            get
            {
                return HttpContext.Current.Request.Url.AbsolutePath.ToLowerInvariant();
            }
        }

        public static page current
        {
            get
            {
                if (!_templatepath.EndsWith(".aspx"))
                {
                    return null;
                }
                page pg = HttpContext.Current.Items["57F8A797-1880-4525-8770-55D9CF77FDB8"] as page;
                if (pg == null)
                {
                    pg = new page {
                        name = "",
                        templatepath = _templatepath
                    };
                    HttpContext.Current.Items["57F8A797-1880-4525-8770-55D9CF77FDB8"] = pg;
                    if (long.TryParse(HttpContext.Current.Request.QueryString["sfcms"], out pg.id))
                    {
                        DataTable dt = new database().ExecuteDataTable("SELECT * FROM SFCMS_Page (NOLOCK) WHERE [ID] = @0 AND [TemplatePath] = @1", new object[] { "@0", pg.id, "@1", _templatepath });
                        if (dt.Rows.Count > 0)
                        {
                            pg.name = Convert.ToString(dt.Rows[0]["Name"]);
                            pg.displayname = Convert.ToString(dt.Rows[0]["DisplayName"]);
                        }
                        else
                        {
                            pg.id = 0L;
                        }
                    }
                }
                return pg;
            }
        }

        public string friendlyurl
        {
            get
            {
                if (string.IsNullOrEmpty(this._friendlyurl))
                {
                    this._friendlyurl = urlrewriter.toasciilower("/" + this.displayname + ".aspx");
                    for (folder parentfolder = base.parent; parentfolder != folder.pageroot; parentfolder = parentfolder.getparent())
                    {
                        this._friendlyurl = "/" + urlrewriter.toasciilower(parentfolder.displayname) + this._friendlyurl;
                    }
                }
                return this._friendlyurl;
            }
        }

        public placeholder[] placeholders
        {
            get
            {
                if (this._placeholders == null)
                {
                    this._placeholders = placeholder.getpageplaceholders();
                }
                return this._placeholders;
            }
        }
    }
}

